package com.tjetc.Impl;

import com.tjetc.domain.*;
import com.tjetc.mapper.*;
import com.tjetc.service.DoctorService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

import java.util.Date;
import java.util.List;

public class DoctorServiceImpl implements DoctorService {
    private Result result = new Result();
    private DoctorMapper doctorMapper = Db.getMapper(DoctorMapper.class);
    private NewbornMapper newbornMapper=Db.getMapper(NewbornMapper.class);
    private VaccinationMapper vaccinationMapper=Db.getMapper(VaccinationMapper.class);
    private DiseaseMapper diseaseMapper=Db.getMapper(DiseaseMapper.class);
    private HealthMapper healthMapper=Db.getMapper(HealthMapper.class);
    private VaccineMapper vaccineMapper=Db.getMapper(VaccineMapper.class);
    @Override
    public Doctor login(String username, String password) {//登录
        Doctor doctorByName = doctorMapper.getDoctorByName(username);
        String password1 = doctorByName.getPassword();
        if (password1.equals(password)){
            return doctorByName;
        }else {
            System.out.println("没有发现这个用户");
            return null;
        }

    }

    @Override
    public Result add(Doctor doctor) {//添加
        String username = doctor.getUsername();
        if (username.equals("")){
            return result.setMsg("用户名不能为空").setCode(500);
        }
        String password = doctor.getPassword();
        if (password.equals("")){
            System.out.println("密码不能为空");
            return result.setMsg("密码不能为空").setCode(500);
        }

        Doctor doctorByName = doctorMapper.getDoctorByName(username);
        if (doctorByName!=null){
            return result.setMsg("用户名已经存在").setCode(500);
        }
        int insert = doctorMapper.insert(doctor);
        if (insert==1){
            return result.setMsg("添加成功").setCode(200);
        }
        return result.setMsg("添加失败").setCode(500);
    }

    @Override
    public Result update(Doctor doctor) {//修改
            String username = doctor.getUsername();
            Doctor doctorByName = doctorMapper.getDoctorByName(username);
            Doctor doctorByid = doctorMapper.selectByPrimaryKey(doctor.getId());
            if (doctorByid==null){
                System.out.println("没有这个用户");
                return result.setCode(500).setMsg("没有这个用户");
            }
            if (doctorByName==null){
                int i = doctorMapper.updateByPrimaryKeySelective(doctor);
                if (i==1){
                    return result.setMsg("修改成功").setCode(200);
                }else return result.setMsg("修改失败").setCode(500);
            }else{
                return result.setMsg("用户名已存在").setCode(500);
            }
        }

    @Override
    public Result delete(int id) {//删除
        Doctor doctor = doctorMapper.selectByPrimaryKey(id);
        if (doctor==null){
            return result.setMsg("没有发现这个用户").setCode(500);
        }
        int i = doctorMapper.deleteByPrimaryKey(id);
        if (i==1){
            return result.setMsg("删除成功").setCode(200);
        }else return result.setMsg("删除失败").setCode(500);
    }

    @Override
    public Doctor getDoctorByName(String userName) {//查询
       return  doctorMapper.getDoctorByName(userName);

    }

    @Override
    public List<Doctor> getDoctorList() {
        return doctorMapper.getDoctorList();
    }

    @Override
    public Doctor getDoctorByNewBornId(int id) {
        return doctorMapper.getDoctorByNewBornId(id);
    }

    @Override
    public String getDoctorByVaccine(int newBornId, int vaccineId) {
        return doctorMapper.getDoctorByVaccine(newBornId,  vaccineId);
    }

    @Override
    public List<Doctor> getDoctorByNewBornIdAll(int id) {//对该孩子接种疫苗的所有医生
        return doctorMapper.getDoctorByNewBornIdAll(id);
    }
    @Override
    public Result findByNameAP(Doctor doctor) {
        Doctor doctor1=doctorMapper.findByName(doctor.getUsername());
        while (true){
            if (doctor1==null){
                return result.setCode(500).setMsg("该用户不存在");
            }else if (!(doctor.getPassword().equals(doctor1.getPassword()))){
                return result.setCode(500).setMsg("密码错误请重新输入");
            }
            break;
        }
        return result.setCode(200).setMsg("登录成功").setData(doctor1);
    }

    @Override
    public Result add(Newborn newborn) {
        try {
            Newborn newborn1=newbornMapper.findByName(newborn.getName());
            if (newborn.equals(newborn1)){
                return result.setCode(500).setMsg("该新生儿已经存在");
            }else {
                int rows=newbornMapper.insert(newborn);
                return rows==1?result.setCode(200).setMsg("添加成功"):result.setCode(500).setMsg("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.setCode(500).setMsg("添加失败");
    }

    @Override
    public Result del(int id) {
        try {
            Newborn newborn=newbornMapper.selectByPrimaryKey(id);
            if (newborn==null){
                return result.setCode(500).setMsg("不存在该新生儿");
            }else {
                int rows=newbornMapper.deleteByPrimaryKey(id);
                return rows==1?result.setCode(200).setMsg("删除成功"):result.setCode(500).setMsg("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.setCode(500).setMsg("删除失败");
    }

    @Override
    public Result upd(Newborn newborn) {
        try {
            Newborn newborn1=newbornMapper.selectByPrimaryKey(newborn.getId());
            if (newborn1==null){
                return result.setCode(500).setMsg("不存在改新生儿");
            }else {
                int rows=newbornMapper.updateByPrimaryKey(newborn);
                return rows==1?result.setCode(200).setMsg("修改成功"):result.setCode(500).setMsg("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.setCode(500).setMsg("修改失败");
    }

    @Override
    public Newborn sel(int id) {

        return newbornMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Newborn> list() {

        return newbornMapper.list();
    }

    @Override
    public List<Vaccination> yimiaoList(int id) {
        return vaccinationMapper.list(id);
    }

    @Override
    public Result upd1(int i) {
        try {
            Vaccination vaccination = vaccinationMapper.selectByPrimaryKey(i);
            if (vaccination.getJiezhongTime() != null) {
                return result.setCode(500).setMsg("该新生儿已经接种疫苗");
            } else {
                Date date = new Date();
                int rows = vaccinationMapper.upd(i, date);
                // 调用减少疫苗数量的方法
                Vaccination vaccination1= vaccinationMapper.selectByPrimaryKey(i);
                int yimiaoId = vaccination1.getYimiaoId();
                int vaccineRows = vaccinationMapper.decreaseVaccineQuantity(yimiaoId);
                if (rows != -1 && vaccineRows != -1) {
                    return result.setCode(200).setMsg("更新成功");
                } else {
                    return result.setCode(500).setMsg("更新失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.setCode(500).setMsg("更新失败");
    }

    @Override
    public List<Disease> serch() {
        return diseaseMapper.getDiseasesWithHealthInfo();
    }

    @Override
    public Doctor findId(int doctorId) {
        return doctorMapper.selectByPrimaryKey(doctorId);
    }

    @Override
    public List<Health> serch1() {
        return healthMapper.getDiseasesWithHealthInfo();
    }
}
